package com.ruoyi.info.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.util.security.Result;
import com.ruoyi.util.security.SandboxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.CmsSandbox;
import com.ruoyi.info.service.ICmsSandboxService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * sandboxController
 * 
 * @author xwj
 * @date 2023-08-07
 */
@Controller
@RequestMapping("/cms/sandbox")
public class CmsSandboxController extends BaseController
{
    private String prefix = "cms/sandbox";

    @Autowired
    private ICmsSandboxService cmsSandboxService;

    @GetMapping()
    public String sandbox()
    {
        return prefix + "/sandbox";
    }

    /**
     * 查询sandbox列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsSandbox cmsSandbox)
    {
        startPage();
        List<CmsSandbox> list = cmsSandboxService.selectCmsSandboxList(cmsSandbox);
        return getDataTable(list);
    }

    /**
     * 导出sandbox列表
     */
    @Log(title = "sandbox", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsSandbox cmsSandbox)
    {
        List<CmsSandbox> list = cmsSandboxService.selectCmsSandboxList(cmsSandbox);
        ExcelUtil<CmsSandbox> util = new ExcelUtil<CmsSandbox>(CmsSandbox.class);
        return util.exportExcel(list, "sandbox数据");
    }

    /**
     * 新增sandbox
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存sandbox
     */
    @Log(title = "sandbox", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsSandbox cmsSandbox)
    {
        return toAjax(cmsSandboxService.insertCmsSandbox(cmsSandbox));
    }

    /**
     * 修改sandbox
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsSandbox cmsSandbox = cmsSandboxService.selectCmsSandboxById(id);
        mmap.put("cmsSandbox", cmsSandbox);
        return prefix + "/edit";
    }

    /**
     * 修改保存sandbox
     */
    @Log(title = "sandbox", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsSandbox cmsSandbox)
    {
        return toAjax(cmsSandboxService.updateCmsSandbox(cmsSandbox));
    }

    /**
     * 删除sandbox
     */
    @Log(title = "sandbox", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsSandboxService.deleteCmsSandboxByIds(ids));
    }

    @PostMapping("/listPage")
    @ResponseBody
    public TableDataInfo listPage(CmsSandbox sandbox){
        startPage();
        SysUser currUser = getSysUser();
        sandbox.setUserId(currUser.getUserId());

        List<CmsSandbox> list = cmsSandboxService.selectCmsSandboxList(sandbox);
        return getDataTable(list);
    }

    @PostMapping( "/uploadApp")
    @ResponseBody
    public AjaxResult uploadApp(MultipartFile file, String os) throws Exception {
        SysUser currUser = getSysUser();
        AjaxResult result = cmsSandboxService.uploadApp(currUser, file, os);
        return result;
    }
}
